package com.dmwork.service.interaction.controller.admin;


import com.dmwork.common.base.result.R;
import com.dmwork.service.interaction.entity.CmsExcellentWork;
import com.dmwork.service.interaction.entity.vo.CmsExcellentWorkVo;
import com.dmwork.service.interaction.service.CmsExcellentWorkService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Transactional
@RestController
@RequestMapping("/admin/dm_work/interaction/cms_excellent_work")
public class AdminCmsExcellentWorkController {

    @Autowired
    private CmsExcellentWorkService cmsExcellentWorkService;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String KEY = "get_excellent_work_list";

    @ApiOperation("新增优秀作品")
    @PostMapping("add_excellent_work")
    public R addExcellentWork(@RequestBody CmsExcellentWork cmsExcellentWork) {
        boolean save = cmsExcellentWorkService.save(cmsExcellentWork);
        if (save) {
            redisTemplate.opsForValue().set(KEY, cmsExcellentWorkService.getExcellentWorkList());
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation("根据id获取指定优秀作品信息")
    @GetMapping("get_excellent_work_by_id/{id}")
    public R getExcellentWorkById(@PathVariable String id) {
        CmsExcellentWorkVo cmsExcellentWorkVo = cmsExcellentWorkService.getExcellentWorkById(id);
        return R.ok().data("item", cmsExcellentWorkVo);
    }

    @ApiOperation("获取所有优秀作品")
    @GetMapping("get_excellent_work_list")
    public R getExcellentWorkList() {
        List<CmsExcellentWorkVo> cmsExcellentWorkVoList = cmsExcellentWorkService.getExcellentWorkList();
        return R.ok().data("rows", cmsExcellentWorkVoList);
    }

    @ApiOperation("修改已有作品的优秀级别")
    @PutMapping("modify_excellent_work")
    public R modifyExcellentWork(@RequestBody CmsExcellentWork cmsExcellentWork) {
        boolean update = cmsExcellentWorkService.updateById(cmsExcellentWork);
        if (update) {
            redisTemplate.opsForValue().set(KEY, cmsExcellentWorkService.getExcellentWorkList());
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("删除优秀作品")
    @DeleteMapping("remove_excellent_work_by_id/{id}")
    public R removeExcellentWorkById(@PathVariable String id) {
        boolean remove = cmsExcellentWorkService.removeById(id);
        if (remove) {
            List<CmsExcellentWorkVo> excellentWorkVoList = cmsExcellentWorkService.getExcellentWorkList();
            if (excellentWorkVoList != null && excellentWorkVoList.size() > 0) {
                redisTemplate.opsForValue().set(KEY, excellentWorkVoList);
            } else {
                redisTemplate.delete(KEY);
            }
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

}
